package com.ruoyi.service.impl;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.result.Result;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.domain.*;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class processServiceImpl implements IprocessService {

    @Autowired
    private IT18Service t18Service;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IT06Service t06Service;

    @Autowired
    private IT24Service t24Service;

    @Autowired
    private IT05Service t05Service;
    @Autowired
    private IT23Service t23Service;
    @Override
    public List<T06> getList(T06 t06, HttpServletRequest request) {

        LoginUser loginUser =tokenService.getLoginUser(request);
        T18 t18 = t18Service.selectT18ByF1801(loginUser.getUserId());
        //获取他的权限等级
        Long auth = t18.getF1809();
        Long comId = t18.getF0301();
        Long deptId = t18.getF0501();
        //查询该用户下的需要审核的人
        t06.setF0301(comId);
//        t06.setF0501(deptId);
        PageUtils.startPage();
        List<T06> alllist =  t06Service.selectNeedProcess(t06);
        List<T06> list = new ArrayList<>();
        for(T06 t06s:alllist){
            if( t06s.getF0612()==2L){
                list.add(t06s);
            }
        }
        return list;
    }

    /**
     * 通过
     *
     * @param f0601s
     * @param request
     * @return
     */
    @Override
    @Transactional
    public Result toProcess(List<Long> f0601s, HttpServletRequest request) {
        if(f0601s.size()<=0){
            return Result.erro("请选择人员");
        }
        LoginUser loginUser =tokenService.getLoginUser(request);

        T18 t18 = t18Service.selectT18ByF1801(loginUser.getUserId());
        Long auth = t18.getF1809();
        for(Long f0601:f0601s){
            T06 t06 = t06Service.selectT06ByF0601(f0601);
            if(ObjectUtils.isEmpty(t06.getF0501())){
                return Result.erro("请先完善个人信息");
            }
            T24 t24 = new T24();
            t24.setF1801(t18.getF1801());
            t24.setF2402(t18.getF1804());
            t24.setF2403(0L);
            t24.setF2404("审核");
            t24.setF2405(t06.getF0614());
            t24.setF2406(new Date());
            t06.setF0610(1L);
            t06.setF0605(0L);
            t06.setF0606(0L);
            t06.setF0612(0L);
            T05 t05 = t05Service.selectT05ByF0501(t06.getF0501());
            if(!ObjectUtils.isEmpty(t05.getF0504())){
                t06.setF0611(t05.getF0504());
            }else{
                t06.setF0611(0L);
            }
            T23 t23 = new T23();
            t23.setF2302("认证申请通过");
            t23.setF2304(t06.getF0602()+"您好，您的认证申请已经通过");
            t23.setF0601(t06.getF0601());
            t23.setF2305(new Date());
            t23.setF0301(t06.getF0301());
            t23.setF2708("运营公司");
            t23.setF2307("运营公司");
            t23.setF2306(0L);
            t24Service.insertT24(t24);
            t06Service.updateT06(t06);
            t23Service.insertT23(t23);
        }
        return Result.success();
    }

    /**
     * 终止
     * @param f0601s
     * @param request
     * @return
     */
    @Override
    @Transactional
    public Result toProcessRefuse(List<Long> f0601s, HttpServletRequest request) {
        if(f0601s.size()<=0){
            return Result.erro("请选择人员");
        }
        LoginUser loginUser =tokenService.getLoginUser(request);
        T18 t18 = t18Service.selectT18ByF1801(loginUser.getUserId());
        Long auth = t18.getF1809();
            for(Long f0601:f0601s){
                T24 t24 = new T24();
                t24.setF1801(t18.getF1801());
                t24.setF2402(t18.getF1804());
                t24.setF2403(1L);
                t24.setF2406(new Date());
                t24.setF2404("审核");
                t06Service.updateProcess(f0601,1L);
                t24Service.insertT24(t24);
            }
        return Result.success();
    }

    /**
     * 驳回
     * @param f0601s
     * @param request
     * @return
     */
    @Override
    @Transactional
    public Result toBack(List<Long> f0601s, HttpServletRequest request) {
        if(f0601s.size()<=0){
            return Result.erro("请选择人员");
        }
        LoginUser loginUser =tokenService.getLoginUser(request);
        T18 t18 = t18Service.selectT18ByF1801(loginUser.getUserId());
        for(Long f0601:f0601s){
            T24 t24 = new T24();
            t24.setF1801(t18.getF1801());
            t24.setF2402(t18.getF1804());
            t24.setF2403(1L);
            t24.setF2406(new Date());
            t24.setF2404("审核");
            t06Service.updateProcess(f0601,4L);
            T06 t06 = t06Service.selectT06ByF0601(f0601);
            t24.setF2405(t06.getF0614());
            t24Service.insertT24(t24);
        }
        return Result.success();
    }

    @Override
    public Result getProcessByF0614(String f0614) {
        List<T24> t24s = t24Service.selectT24ByF2405(f0614);
        return Result.success(t24s,"查询成功");
    }
}
